package queries;

import controller.LollaWrapper;
import entities.Event;
import entities.Events;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class EventQueries extends AbstractQueries {
    private long pageCount;
    private final String baseEventUrl;
    private final LollaWrapper wrap;

    public EventQueries(LollaWrapper wrap) throws IOException {
        this.wrap = wrap;
        baseEventUrl = wrap.createFullAPIUrl("events");
        Events events = wrap.makeCall(baseEventUrl, Events.class);
        pageCount = events.getTotalPages();
    }

    private List<Event> getEventsByPage(int page) throws IOException {
        String url = baseEventUrl + "&page=" + page;
        return wrap.makeCall(url, Events.class).getEvents();
    }

    public List<Event> getAllEvents() throws IOException {
        List<Event> allEvents = new ArrayList<Event>();
        for (int i = 1; i <= pageCount; i++) {
            allEvents.addAll(getEventsByPage(i));
        }
        return allEvents;
    }

    public Event getEventById(long id) throws IOException {
        return wrap.makeCall(wrap.createFullAPIUrl("events/" + id), Event.class);
    }

    public long getEventVoteCountById(long id) throws IOException {
        return wrap.makeCall(wrap.createFullAPIUrl("events/" + id + "/current_votes"), Event.class).getVotes();
    }

}
